package ext.generic.doc.processors;

import com.ptc.netmarkets.util.beans.NmCommandBean;

import wt.doc.WTDocument;

import wt.fc.Persistable;
import wt.fc.collections.WTArrayList;

import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.core.components.forms.*;

import java.util.*;

import wt.util.WTException;

import wt.workflow.work.WorkItem;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.engine.ProcessData;

import wt.session.SessionHelper;

import wt.util.WTMessage;
import wt.util.LocalizableMessage;

import com.ptc.netmarkets.util.misc.NmContext;

import wt.method.RemoteAccess;

import java.io.Serializable;

import com.ptc.netmarkets.model.NmOid;

import ext.generic.doc.DocWorkflowUtil;

import wt.identity.DisplayIdentity;
import wt.identity.IdentityFactory;

import wt.doc.WTDocumentMaster;

/**
 * 文档流程中"收集随签对象"的action的响应方法类;
 */
public class CollectDocumentFormProcessor extends DefaultObjectFormProcessor implements RemoteAccess, Serializable
{

    public CollectDocumentFormProcessor()
    {
    }

    /**
     *具体的"收集随签对象(文档)"的方法
     * @param nmcommandbean
     * @param list
     * @return
     * @throws WTException
     */
    public FormResult postProcess(NmCommandBean nmcommandbean, List list) throws WTException
    {
        WTArrayList batchReviewDocs = new WTArrayList();
        Vector existVec = new Vector();
        Vector errorstate1 = new Vector();
        Vector errorstate2 = new Vector();
        Vector partdoc = new Vector();
        Vector addedVec = new Vector();
        NmOid nmoid = nmcommandbean.getPrimaryOid();
        WorkItem workitem = (WorkItem) nmoid.getRef();
        WfAssignedActivity wfassignedactivity = (WfAssignedActivity) workitem.getSource().getObject();
        ProcessData context = wfassignedactivity.getContext();
        batchReviewDocs = (WTArrayList) (context.getValue("reviewObjs"));
        WTDocument pbo = (WTDocument) batchReviewDocs.getPersistable(0);
       // WTDocument pbo = (WTDocument) batchReviewDocs.get(0);
        String pbomodifer = pbo.getModifierName();
        java.util.Locale locale = SessionHelper.getLocale();
        ArrayList arraylist = nmcommandbean.getSelected();
        if (arraylist.size() == 0)
        {
            FormResult formresult = new FormResult();
            FeedbackMessage feedbackmessage =
                new FeedbackMessage(FeedbackType.FAILURE, locale, new WTMessage("ext.generic.doc.docResource", "NO_REVIEWDOC_SELECTED", null), null, new LocalizableMessage[]
                    { new WTMessage("ext.generic.doc.docResource", "NO_REVIEWDOC_SELECTED", null) });
            formresult.addFeedbackMessage(feedbackmessage);
            formresult.setStatus(FormProcessingStatus.FAILURE);
            formresult.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
            
            return formresult;
        }
        else
        {
            for (int i = 0; i < arraylist.size(); i++)
            {

                NmContext nmcontext = (NmContext) arraylist.get(i);
                Persistable persistable = nmcontext.getTargetOid().getWtRef().getObject();
                if (!(persistable instanceof WTDocument))
                    continue;
                WTDocument wtdocument = (WTDocument) nmcontext.getTargetOid().getWtRef().getObject();
                Vector subDocList = new Vector();
                subDocList = DocWorkflowUtil.getFirstLevelSubDocListByDoc(wtdocument);
                if (subDocList.size() > 0)
                {
                    for (int j = 0; j < subDocList.size(); j++)
                    {
                        WTDocument subdoc = (WTDocument) subDocList.elementAt(j);
                        System.out.println("****subdoc=" + subdoc.getIdentity());
                        String modifername = subdoc.getModifierName();
                        String state = subdoc.getLifeCycleState().toString();
                        boolean ispartdoc = DocWorkflowUtil.isReviewDoc(subdoc, "随签", "doc");
                        if (ispartdoc)
                        {
                            if (!partdoc.contains(subdoc))
                                partdoc.add(subdoc);
                        }
                        else if (batchReviewDocs.contains(subdoc))
                        {
                            if (!existVec.contains(subdoc))
                                existVec.add(subdoc);
                        }
                        else if (modifername.equalsIgnoreCase(pbomodifer))
                        {
                            if (state.equalsIgnoreCase("INWORK") || state.equalsIgnoreCase("WAITREVIEW"))
                            {
                                batchReviewDocs.add(subdoc);
                                addedVec.add(subdoc);
                            }
                            else if (!errorstate1.contains(subdoc))
                                errorstate1.add(subdoc);
                        }
                        else if (state.equalsIgnoreCase("WAITREVIEW"))
                        {
                            batchReviewDocs.add(subdoc);
                            addedVec.add(subdoc);
                        }
                        else if (!errorstate2.contains(subdoc))
                            errorstate2.add(subdoc);
                    }
                }
                Vector relateddoc = new Vector();
                relateddoc = DocWorkflowUtil.getRelatedDocsOfDoc(wtdocument);
                if (relateddoc.size() > 0)
                {
                    for (int k = 0; k < relateddoc.size(); k++)
                    {
                        WTDocument reldoc = (WTDocument) relateddoc.elementAt(k);
                        System.out.println("&&&&&&&&&&&&&&reldoc=" + reldoc.getIdentity());
                        String mname = reldoc.getModifierName();
                        String lc = reldoc.getLifeCycleState().toString();
                        boolean ispartdoc1 = DocWorkflowUtil.isReviewDoc(reldoc, "随签", "doc");
                        System.out.println("ispartdoc1=" + ispartdoc1);
                        if (ispartdoc1)
                        {
                            if (!partdoc.contains(reldoc))
                                partdoc.add(reldoc);
                        }
                        else if (batchReviewDocs.contains(reldoc))
                        {
                            if (!existVec.contains(reldoc))
                                existVec.add(reldoc);
                        }
                        else if (mname.equalsIgnoreCase(pbomodifer))
                        {
                            if (lc.equalsIgnoreCase("INWORK") || lc.equalsIgnoreCase("WAITREVIEW"))
                            {
                                System.out.println("batchReviewDocs.add(" + reldoc.getIdentity() + ")");
                                batchReviewDocs.add(reldoc);
                                addedVec.add(reldoc);
                            }
                            else if (!errorstate1.contains(reldoc))
                                errorstate1.add(reldoc);
                        }
                        else if (lc.equalsIgnoreCase("WAITREVIEW"))
                        {
                            batchReviewDocs.add(reldoc);
                            addedVec.add(reldoc);
                        }
                        else if (!errorstate2.contains(reldoc))
                            errorstate2.add(reldoc);
                    }
                }
            }
            System.out.println("addedVec=" + addedVec + "	/n" + "existVec=" + existVec + "	/n" + "errorstate1=" + errorstate1 + "	/n" + "errorstate2=" + errorstate1 + "	/n" +
                               "partdoc=" + partdoc);
            if ((addedVec.size() == 0) & (existVec.size() == 0) & (errorstate1.size() == 0) & (errorstate2.size() == 0) & (partdoc.size() == 0))
            {
                FormResult formresult3 = new FormResult();
                FeedbackMessage feedbackmessage3 =
                    new FeedbackMessage(FeedbackType.FAILURE, locale, new WTMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE", null), null,
                                        new LocalizableMessage[]
                        { new WTMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_EMPTY_MSG", null) });
                formresult3.addFeedbackMessage(feedbackmessage3);
                formresult3.setStatus(FormProcessingStatus.SUCCESS);
                formresult3.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
                return formresult3;
            }
            else
            {
                context.setValue("reviewObjs", batchReviewDocs);
                wfassignedactivity.setContext(context);
                wt.fc.PersistenceHelper.manager.save(wfassignedactivity);
                FormResult formresult1 = new FormResult();
                formresult1.addFeedbackMessage(getAddFbMsg(FeedbackType.FAILURE, addedVec, existVec, errorstate1, errorstate2, partdoc, locale));
                formresult1.setStatus(FormProcessingStatus.SUCCESS);
                formresult1.setNextAction(FormResultAction.REFRESH_CURRENT_PAGE);
                super.postProcess(nmcommandbean, list);
                return formresult1;
            }

        }
    }
    protected FormResult setRefreshInfo(FormResult formresult, NmCommandBean nmcommandbean, List list) throws WTException
    {
        DynamicRefreshInfo dynamicrefreshinfo = new DynamicRefreshInfo();
        dynamicrefreshinfo.setLocation(nmcommandbean.getActionOid());
        dynamicrefreshinfo.setOid(nmcommandbean.getActionOid());
        NmCommandBean _tmp = nmcommandbean;
        dynamicrefreshinfo.setAction("U");
        formresult.addDynamicRefreshInfo(dynamicrefreshinfo);
        return formresult;
    }

    public static FeedbackMessage getAddFbMsg(FeedbackType feedbacktype, List list1, List list2, List list3, List list4, List list5, Locale locale) throws WTException
    {
        String s = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE", null);
        String s1 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE_MSG1", null);
        String s2 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE_MSG2", null);
        String s3 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE_MSG3", null);
        String s4 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE_MSG4", null);
        String s5 = WTMessage.getLocalizedMessage("ext.generic.doc.docResource", "COLLECT_REVIEWDOC_COMPLETE_MSG5", null);
        return createFeedbackMessage(feedbacktype, locale, s, s1, s2, s3, s4, s5, list1, list2, list3, list4, list5);
    }

    public static FeedbackMessage createFeedbackMessage(FeedbackType feedbacktype, Locale locale, String s, String s1, String s2, String s3, String s4, String s5, List list1,
                                                        List list2, List list3, List list4, List list5) throws WTException
    {
        StringBuffer stringbuffer = new StringBuffer();
        if (list1.size() > 0)
        {
            stringbuffer.append(s1);
            stringbuffer.append("\n");
            for (int i = 0; i < list1.size(); i++)
            {
                Object obj1 = list1.get(i);
                DisplayIdentity displayidentity1 = IdentityFactory.getDisplayIdentity(obj1);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity1.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }

        if (list2.size() > 0)
        {
            stringbuffer.append(s2);
            stringbuffer.append("\n");
            for (int j = 0; j < list2.size(); j++)
            {
                Object obj2 = list2.get(j);
                DisplayIdentity displayidentity2 = IdentityFactory.getDisplayIdentity(obj2);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity2.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }

        if (list3.size() > 0)
        {
            stringbuffer.append(s3);
            stringbuffer.append("\n");
            for (int k = 0; k < list3.size(); k++)
            {
                Object obj3 = list3.get(k);
                DisplayIdentity displayidentity3 = IdentityFactory.getDisplayIdentity(obj3);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity3.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }

        if (list4.size() > 0)
        {
            stringbuffer.append(s4);
            stringbuffer.append("\n");
            for (int l = 0; l < list4.size(); l++)
            {
                Object obj4 = list4.get(l);
                DisplayIdentity displayidentity4 = IdentityFactory.getDisplayIdentity(obj4);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity4.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }

        if (list5.size() > 0)
        {
            stringbuffer.append(s5);
            stringbuffer.append("\n");
            for (int m = 0; m < list5.size(); m++)
            {
                Object obj5 = list5.get(m);
                DisplayIdentity displayidentity5 = IdentityFactory.getDisplayIdentity(obj5);
                stringbuffer.append("\n   ");
                stringbuffer.append(displayidentity5.getLocalizedMessage(locale));
            }
            stringbuffer.append("\n\n");
        }
        FeedbackMessage feedbackmessage = new FeedbackMessage(feedbacktype, locale, s, null, new String[]
                { stringbuffer.toString() });
        return feedbackmessage;
    }
}
